package com.fortune.controller;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import com.fortune.entity.MUserInfo;
import org.hibernate.validator.constraints.NotBlank;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.fortune.base.BaseAPIExcution;
import com.fortune.constants.Constants;
import com.fortune.dto.excution.MSelectExcution;
import com.fortune.entity.DUserInfo;
import com.fortune.enums.MSelectEnum;
import com.fortune.service.DSelectService;
import com.fortune.service.MSelectService;
import com.fortune.vo.dSelect.DSelectProduct;
import com.fortune.vo.dSelect.MDiscount;
import com.fortune.vo.mSelect.SelectProduct;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

@Api(value = "dSelect", description = "经销商商下拉框数据", tags = {"经销商下拉框数据"})
@RestController
@RequestMapping("dSelect")
@Validated
public class DSelectController {
	@Autowired
	private DSelectService dSelectService;
	@Autowired
	private MSelectService mSelectService;
	
	@ApiOperation(value = "商品列表(某个仓库下有库存的商品)", nickname = "dSelectStoreProducts", notes = "", tags={ "@曹然" })
	@ApiImplicitParam(name="storeId", value="仓库id",paramType="query",dataType="string")
    @GetMapping(value = "storeProducts",
            produces = { MediaType.APPLICATION_JSON_VALUE })
	public BaseAPIExcution<List<DSelectProduct>> listProducts(HttpServletRequest request, @Validated @NotBlank(message="仓库id不能为空")String storeId){
		HttpSession session = request.getSession();
		DUserInfo dUserInfo = (DUserInfo) session.getAttribute(Constants.SESSION_D_USER);
		String dId = dUserInfo.getdId();
		List<DSelectProduct> products = dSelectService.listStoreProduncts(dId,storeId);
		return new MSelectExcution<List<DSelectProduct>>(MSelectEnum.SUCCESS, products);
	}

	@ApiOperation(value = "商品列表(某个仓库下有库存的商品,厂商)", nickname = "dSelectStoreProducts", notes = "", tags={ "@安童" })
	@ApiImplicitParam(name="storeId", value="仓库id",paramType="query",dataType="string")
	@GetMapping(value = "storeProductCS",
			produces = { MediaType.APPLICATION_JSON_VALUE })
	public BaseAPIExcution<List<DSelectProduct>> storeProductCS(HttpServletRequest request, @Validated @NotBlank(message="仓库id不能为空")String storeId){
		HttpSession session = request.getSession();
		MUserInfo mUserInfo = (MUserInfo) session.getAttribute(Constants.SESSION_M_USER);
		String mid = mUserInfo.getmId();
		List<DSelectProduct> products = dSelectService.listStoreProdunctCS(mid,storeId);
		return new MSelectExcution<List<DSelectProduct>>(MSelectEnum.SUCCESS, products);
	}
	
	@ApiOperation(value = "商品列表(某个厂商提供的所有商品)", nickname = "dSelectProduct", notes = "", tags={ "@曹然" })
	@ApiImplicitParam(name="mId", value="厂商id(如果为空则返回自营商品)",paramType="query",dataType="string")
    @GetMapping(value = "products",
            produces = { MediaType.APPLICATION_JSON_VALUE })
	public BaseAPIExcution<List<DSelectProduct>> listMProducts(HttpServletRequest request, String mId){
		HttpSession session = request.getSession();
		DUserInfo dUserInfo = (DUserInfo) session.getAttribute(Constants.SESSION_D_USER);
		if(mId!=null&&!mId.equals("")) {
			List<SelectProduct> productss = mSelectService.listAllMProducts(mId, dUserInfo.getdId());
			List<DSelectProduct> products =new ArrayList<>();
			for (SelectProduct selectProduct : productss) {
				DSelectProduct dSelectProduct = new DSelectProduct();
				dSelectProduct.setId(selectProduct.getId());
				dSelectProduct.setName(selectProduct.getName());
				dSelectProduct.setPrice(selectProduct.getPrice());
				dSelectProduct.setTargetId(mId);
				dSelectProduct.setNumber(selectProduct.getNumber());
				dSelectProduct.setTargetType("M");
				BigDecimal boxPrice = dSelectService.getBoxPriceByMIdAndId(dSelectProduct.getTargetId(), dSelectProduct.getId());
				dSelectProduct.setBoxPrice(boxPrice);
				products.add(dSelectProduct);
			}
			return new MSelectExcution<List<DSelectProduct>>(MSelectEnum.SUCCESS, products);
		}else {
			List<DSelectProduct> products = dSelectService.listProductByDId(dUserInfo.getdId());
			return new MSelectExcution<List<DSelectProduct>>(MSelectEnum.SUCCESS, products);
		}
	}
	
	@ApiOperation(value = "获取优惠政策(结果data为空或undifine则代表没有优惠政策)", nickname = "dSelectDiscount", notes = "", tags={ "@曹然" })
	@ApiImplicitParams({
		@ApiImplicitParam(name="mId", value="厂商id",paramType="query",dataType="string"),
		@ApiImplicitParam(name="id", value="商品id",paramType="query",dataType="string")
	})
    @GetMapping(value = "discount",
            produces = { MediaType.APPLICATION_JSON_VALUE })
	public BaseAPIExcution<MDiscount> discount(HttpServletRequest request, @Validated @NotBlank(message="厂商id不能为空")String mId, @Validated @NotBlank(message="商品id不能为空")String id){
		MDiscount mDiscount = dSelectService.getDiscountByMIdAndId(mId, id);
		return new MSelectExcution<MDiscount>(MSelectEnum.SUCCESS, mDiscount);
	}
	
//	@ApiOperation(value = "获取空箱押金", nickname = "dSelectBoxPrice", notes = "", tags={ "@曹然" })
//	@ApiImplicitParams({
//		@ApiImplicitParam(name="mId", value="厂商id,为空则代表自营产品",paramType="query",dataType="string"),
//		@ApiImplicitParam(name="id", value="商品id",paramType="query",dataType="string")
//	})
//    @GetMapping(value = "boxPrice",
//            produces = { MediaType.APPLICATION_JSON_VALUE })
	@Deprecated
	public BaseAPIExcution<BigDecimal> boxPrice(HttpServletRequest request,String mId, @Validated @NotBlank(message="商品id不能为空")String id){
		if (mId!=null&&!mId.equals("")) {
			BigDecimal bigDecimal = dSelectService.getBoxPriceByMIdAndId(mId, id);
			if (bigDecimal==null) {
				return new MSelectExcution<BigDecimal>(MSelectEnum.PRODUCT_NOT_EXIST, null);
			}
			return new MSelectExcution<BigDecimal>(MSelectEnum.SUCCESS, bigDecimal);
		}
		HttpSession session = request.getSession();
		DUserInfo dUserInfo = (DUserInfo) session.getAttribute(Constants.SESSION_D_USER);
		String dId = dUserInfo.getdId();
		BigDecimal bigDecimal = dSelectService.getBoxPriceByDIdAndId(dId,id);
		if (bigDecimal==null) {
			return new MSelectExcution<BigDecimal>(MSelectEnum.PRODUCT_NOT_EXIST, null);
		}
		return new MSelectExcution<BigDecimal>(MSelectEnum.SUCCESS, bigDecimal);
	}
}
